package com.sctvcloud.bazhong.ui.adapter.holder;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomCheckBox;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.beans.VoteOption;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyu on 2017/8/17.
 */

public class VoteHolder extends BaseRecylerHolder<VoteOption> {

    @BindView(R.id.item_vote_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_vote_percent)
    protected CustomFontTextView percent;
    @BindView(R.id.item_vote_count)
    protected CustomFontTextView count;
    @BindView(R.id.item_vote_selector)
    protected CustomCheckBox selector;
    @BindView(R.id.item_vote_progress)
    protected ProgressBar progressBar;
    @BindView(R.id.item_vote_lay_pro)
    protected View layProgress;

    private int total;
    private boolean canSelector;
    private int mode;

    public VoteHolder(Context context, final View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (canSelector) {
                    selector.setChecked(!selector.isChecked());
                }
            }
        });
        selector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (internalCheckChangedListener != null) {
                    internalCheckChangedListener.onItemInternalCheckedChanged(itemView, buttonView, getAdapterPosition(), isChecked);
                }
            }
        });
    }
    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }
    @Override
    public void setData(VoteOption voteOption) {
        title.setText(voteOption.getVoteOptionTitle());
        if (canSelector) {
            layProgress.setVisibility(View.GONE);
            selector.setVisibility(View.VISIBLE);
            if (mode == 0) {
                selector.setButtonDrawable(R.drawable.select_news_radio);
            } else {
                selector.setButtonDrawable(R.drawable.select_news_checker);
            }
        } else {
            layProgress.setVisibility(View.VISIBLE);
            progressBar.setProgress(voteOption.getSelectedCount());
            selector.setVisibility(View.GONE);
            count.setText(context.getString(R.string.news_writ_, voteOption.getSelectedCount()));
            percent.setText(context.getString(R.string.percentage_
                    , voteOption.getSelectedCount() * 100F / total));
        }
    }

    @Override
    public void setData(VoteOption voteOption, int position, boolean isSelected) {
        super.setData(voteOption, position, isSelected);
        selector.setChecked(isSelected, false);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        internalCheckChangedListener = null;
    }

    public void setDataBefor(int total, int mode, boolean canSelector) {
        boolean b = this.total != total;
        this.total = total;
        this.mode = mode;
        this.canSelector = canSelector;
        if (b && progressBar != null) {
            progressBar.setMax(total);
        }
    }
}
